package com.fg.cloud.factory.controller.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.ProblemStatus;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.FactoryEntity;
import com.fg.cloud.manager.entry.system.ProblemInteractiveEntity;
import com.fg.cloud.manager.entry.system.SystemProblemEntity;
import com.fg.cloud.manager.service.factory.FactoryService;
import com.fg.cloud.manager.service.system.SystemProblemService;

@Controller
@RequestMapping("/systemPro")
public class FactoryProblemController extends BaseController {
	
	@Autowired
	private SystemProblemService systemProblemService;
	
	@Autowired
	private FactoryService factoryService;

	/**
	 * Description:厂商补充问题反馈
	 * @param req
	 * @param resp
	 * @param id
	 * @param text
	 * @author dingjiang
	 * @date 2017年9月5日上午10:06:33
	 */
	@RequestMapping(value="/replenishFac",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void replenishFac(HttpServletRequest req, HttpServletResponse resp, Long id, String text){
		try {
			//验证反馈ID非空
			if (LongUtils.isEmpty(id)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请选择问题");
				return ;
			}
			SystemProblemEntity systemProblemEntity = systemProblemService.findByFac(id, getCurrentFactoryUser().getFacId());
			//验证反馈id是否合法
			if ( null == systemProblemEntity ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			//判断问题状态
			if ( systemProblemEntity.getStatus() == ProblemStatus.CLOSED ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题已关闭");
				return ;
			}
			//验证补充内容非空
			if (StringUtils.isEmpty(text)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请输入补充内容");
				return ;
			}
			//补充问题
			ProblemInteractiveEntity interactive = new ProblemInteractiveEntity();
			interactive.setType((byte) 2);
			interactive.setContent(text);
			interactive.setProId(id);
			interactive.setCreator(getCurrentFactoryUser().getId());
			interactive.setUpdater(getCurrentFactoryUser().getId());
			systemProblemService.reply(interactive);
			//修改问题状态
			systemProblemEntity.setStatus(ProblemStatus.PENDING);
			systemProblemEntity.setUpdater(getCurrentFactoryUser().getId());
			systemProblemService.updateByPrimaryKeySelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	
	/**
	 * Description:厂商反馈新问题给系统管理员
	 * @param req
	 * @param resp
	 * @param systemProblemEntity
	 * @author dingjiang
	 * @date 2017年9月4日下午2:48:57
	 */
	@RequestMapping(value="/add",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void add(HttpServletRequest req, HttpServletResponse resp, SystemProblemEntity systemProblemEntity){
		try {
			//验证参数非空
			if (null == systemProblemEntity.getTitle() || StringUtils.isEmpty(systemProblemEntity.getTitle())) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "标题不能为空");
				return ;
			}
			if (null == systemProblemEntity.getContent() || StringUtils.isEmpty(systemProblemEntity.getContent())) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "内容不能为空");
				return ;
			}
			//判重
			systemProblemEntity.setFacId(getCurrentFactoryUser().getFacId());
			if ( systemProblemService.selectBycondition(systemProblemEntity) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题记录已存在");
				return ;
			}
			//设置问题类型
			systemProblemEntity.setType(ProblemStatus.SYSPROBLEM);
			FactoryEntity currentFac = factoryService.find( getCurrentFactoryUser().getFacId() );
			systemProblemEntity.setFeederName( currentFac.getFacName() );
			systemProblemEntity.setPhone( getCurrentFactoryUser().getPhone() );
			systemProblemEntity.setCreator( getCurrentFactoryUser().getId() );
			systemProblemEntity.setUpdater( getCurrentFactoryUser().getId() );
			systemProblemService.insertSelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	
	/**
	 * Description:厂商平台可获取下属经销商所有的问题
	 * @param req
	 * @param resp
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @author dingjiang
	 * @date 2017年8月23日下午1:36:47
	 */
	@RequestMapping(value="/selectforPage",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectforPage(HttpServletRequest req, HttpServletResponse resp, Integer page, Integer rows, 
			String sort, String order, Byte status ){
		try {
			if (null == page) {
				page = 1;
			}
			if (null == rows) {
				rows = 10;
			}
			PageUtils<SystemProblemEntity> pageinfo = new PageUtils<SystemProblemEntity>(page, rows, sort, order);
			pageinfo.setSort("status");
			pageinfo.setOrder("desc");
			if ( status != null ) {
				pageinfo.getCondition().put("status =  ", status);
			}
			pageinfo.getCondition().put("facId = ", getCurrentFactoryUser().getFacId());			
			pageinfo.getCondition().put("type = ", ProblemStatus.BUSIPROBLEM);			
			systemProblemService.selectforPage(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}

	
	/**
	 * Description:厂商处理业务问题（回复）
	 * @param req
	 * @param resp
	 * @param id
	 * @author dingjiang
	 * @date 2017年8月23日下午3:09:13
	 */
	@RequestMapping(value="/handle",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void handle(HttpServletRequest req, HttpServletResponse resp, Long id, String text){
		try {
			//验证反馈ID非空
			if (LongUtils.isEmpty(id)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请选择问题");
				return ;
			}
			SystemProblemEntity systemProblemEntity = systemProblemService.findByFac(id, getCurrentFactoryUser().getFacId());
			//验证反馈ID是否合法
			if ( null == systemProblemEntity || ProblemStatus.SYSPROBLEM == systemProblemEntity.getType() ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			//判断问题状态
			if ( systemProblemEntity.getStatus() == ProblemStatus.PROCESSED ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题已处理");
				return ;
			}
			if ( systemProblemEntity.getStatus() == ProblemStatus.CLOSED ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题已关闭");
				return ;
			}
			//验证回复内容非空
			if (StringUtils.isEmpty(text)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请输入回复内容");
				return ;
			}
			//回复经销商
			ProblemInteractiveEntity interactive = new ProblemInteractiveEntity();
			interactive.setType((byte) 1);
			interactive.setContent(text);
			interactive.setProId(id);
			interactive.setCreator(getCurrentFactoryUser().getId());
			interactive.setUpdater(getCurrentFactoryUser().getId());
			systemProblemService.reply(interactive);
			//修改问题状态
			systemProblemEntity.setStatus(ProblemStatus.PROCESSED);
			systemProblemEntity.setUpdater(getCurrentFactoryUser().getId());
			systemProblemService.updateByPrimaryKeySelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}	
	}

	
	/**
	 * Description:厂商用户查看问题详情
	 * @param req
	 * @param resp
	 * @param id
	 * @author dingjiang
	 * @date 2017年8月23日下午3:29:16
	 */
	@RequestMapping(value="/selectDetails",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectDetails(HttpServletRequest req, HttpServletResponse resp,  @Param("id")Long id ){
		try {
			//验证非空
			if (LongUtils.isEmpty(id)) {
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_NULL);
				return ;
			}
			//判断输入id是否合法
			SystemProblemEntity systemProblemEntity = systemProblemService.findByFac(id,getCurrentFactoryUser().getFacId());
			if ( null == systemProblemEntity ) {
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			//判断问题是否已关闭
			if ( systemProblemEntity.getStatus() == ProblemStatus.CLOSED ) {
				doResult(resp, false, StatusCode.FINDERROR, null, "问题已关闭");
				return ;
			}
			doResult(resp, true, StatusCode.FINDSUCCESS, systemProblemEntity, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}

	
	/**
	 * Description:厂商用户关闭问题(只能关闭自己发起的问题)
	 * @param req
	 * @param resp
	 * @param id
	 * @author dingjiang
	 * @date 2017年8月23日下午2:59:47
	 */
	@RequestMapping(value="/supClose",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void supClose(HttpServletRequest req, HttpServletResponse resp, @Param("id")Long id){
		try {
			//验证非空
			if (LongUtils.isEmpty(id)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_NULL);
				return ;
			}
			//判断输入id是否合法
			SystemProblemEntity systemProblemEntity = systemProblemService.findByFac(id, getCurrentFactoryUser().getFacId());
			if ( null == systemProblemEntity || getCurrentFactoryUser().getId() != systemProblemEntity.getCreator() ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			systemProblemEntity.setStatus(ProblemStatus.CLOSED);
			systemProblemEntity.setUpdater(getCurrentFactoryUser().getId());
			systemProblemService.updateByPrimaryKeySelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}	
	}
	
	
}
